package com.gym.utils;


import java.io.*;
import java.util.Properties;

import static com.gym.utils.Serializes.closeQuietly;
import static java.io.File.separator;
/**
 * @author Gym
 */
public class Props {

	public static Properties tryProperties(String propertiesFileName, String userHomeBasePath) {
		Properties properties = new Properties();
		InputStream is = null;
		try {
			is = Props.tryResource(propertiesFileName, userHomeBasePath, Silent.ON);
			if (is != null) {
                properties.load(is);
            }
		} catch (IOException e) {
		} finally {
			closeQuietly(is);
		}

		return properties;
	}

	enum Silent {
		ON, OFF
	}

	public static InputStream tryResource(String propertiesFileName, String userHomeBasePath, Silent silent) {
		InputStream is = currentDirResource(new File(propertiesFileName));
		if (is != null) {
            return is;
        }

		is = userHomeResource(propertiesFileName, userHomeBasePath);
		if (is != null) {
            return is;
        }

		is = classpathResource(propertiesFileName);
		if (is != null || silent == Silent.ON) {
            return is;
        }

		throw new RuntimeException("fail to find " + propertiesFileName + " in current dir or classpath");
	}

	private static InputStream userHomeResource(String pathname, String appHome) {
		String filePath = System.getProperty("user.home") + separator + appHome;
		File dir = new File(filePath);
		if (!dir.exists()) {
            return null;
        }

		return currentDirResource(new File(dir, pathname));
	}

	private static InputStream currentDirResource(File file) {
		if (!file.exists()) {
            return null;
        }

		try {
			return new FileInputStream(file);
		} catch (FileNotFoundException e) {
			return null;
		}
	}

	public static InputStream classpathResource(String resourceName) {
		return Props.class.getClassLoader().getResourceAsStream(resourceName);
	}

}
